package com.chixing.controller;

import com.chixing.pojo.Customer;
import com.chixing.pojo.LoginReqParams;
import com.chixing.service.CustomerService;
import com.chixing.util.MsgUtil;
import com.chixing.util.RandomUtil;
import com.chixing.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@RestController
public class CustomerController{
    // 保存短信用
    private Map<String, String> storage = new ConcurrentHashMap<>();

    @Autowired
    CustomerService customerService;
    @RequestMapping("loginGetCode")
    public ServerResponse loginGetCode(@RequestBody LoginReqParams loginReqParams,HttpSession session){

        ServerResponse serverResponse = loginGetMsg(loginReqParams.getMobilePhone(), session);
        System.out.println(serverResponse);

         return serverResponse;
    }

    @RequestMapping("registerGetCode")
    public ServerResponse registerGetCode(@RequestBody LoginReqParams loginReqParams,HttpSession session){

        ServerResponse serverResponse = registerGetMsg(loginReqParams.getMobilePhone(), session);
        System.out.println(serverResponse);

        return serverResponse;
    }

    @RequestMapping("codeLogin")
    public ServerResponse codeLogin(@RequestBody LoginReqParams loginReqParams,HttpSession session){

        ServerResponse serverResponse = doCodeLogin(loginReqParams.getMobilePhone(),session);
        System.out.println(serverResponse);

        return serverResponse;
    }

    @RequestMapping("pwdLogin")
    public ServerResponse pwdLogin(@RequestBody LoginReqParams loginReqParams,HttpSession session){
        ServerResponse serverResponse = doPwdLogin(loginReqParams.getNumber(),session);
        System.out.println(serverResponse);

        return serverResponse;
    }

    @RequestMapping("register")
    public ServerResponse register(@RequestBody LoginReqParams loginReqParams,HttpSession session){
        ServerResponse serverResponse = doRegister(loginReqParams.getMobilePhone(),loginReqParams.getPassWord(),session);
        System.out.println(serverResponse);
        return serverResponse;
    }

    @RequestMapping("outLogin")
    public void outLogin(HttpSession session){
        Object loginCustomer = session.getAttribute("loginCustomer");
        if (loginCustomer!=null){
            loginCustomer=null;
            session.setAttribute("loginCustomer",loginCustomer);
        }
    }

    @RequestMapping("getCustImg")
    public String getCustImg( HttpSession session){
        Object loginCustomer = session.getAttribute("loginCustomer");
        Customer customer = (Customer) loginCustomer;
        System.out.println(customer);
        if (customer!=null){
            String custImg = customer.getCustImg();
//        System.out.println(custImg);
            session.setAttribute("custImg",custImg);
            return custImg;
        }
        return null;
    }

    @RequestMapping("jump")
    public void customerByJump(HttpSession session){
        Object loginCustomer = session.getAttribute("loginCustomer");
        System.out.println(loginCustomer);
        session.setAttribute("loginCustomer",loginCustomer);
//        return session;
    }


    /**
     * 登录
     * @return
     */
    private ServerResponse doCodeLogin(String mobilePhone,HttpSession session){
        ServerResponse byTelno = customerService.getByTelno(Long.valueOf(mobilePhone));
        if(byTelno.getCode() ==200){
//            // 2.根据手机号获取保存的短信验证码
//            String code = storage.get(mobilePhone);
//            // 3.短信码比较
//            if (msgCode.equals(code)) {
//                // 验证成功
            session.setAttribute("loginCustomer",byTelno.getData());
            System.out.println("登录成功");

        }return byTelno;

    }

    private ServerResponse doPwdLogin(Long number,HttpSession session){
        ServerResponse getByCustNo = customerService.getByCustNo(number);
        ServerResponse byCustNo = customerService.getPwdByCustNo(number);
        System.out.println(byCustNo);
        if(byCustNo.getCode() ==200){
            session.setAttribute("loginCustomer",getByCustNo.getData());
        }
        return byCustNo;
    }

    /**
     * 获取短信验证码
     * @return
     */
    private ServerResponse loginGetMsg(String mobilePhone ,HttpSession session)   {
        // 返回参数
        Map<String, String> map = new HashMap<>();
        // 0 表示成功
        map.put("resCode", "0");

        ServerResponse byTelno = customerService.getByTelno(Long.valueOf(mobilePhone));
        if (byTelno.getCode() == 200) {
            // 生产随机短信码
            String code = RandomUtil.createRandNum();
            System.out.println("------ 验证码：" + code);

            // 2.使用工具类获取短信
            MsgUtil.sendMsg(mobilePhone, code);
            System.out.println("短信验证码："+code);//0
            // 3.判断短信发送结果
//            if (returnCode != 0) {
//                // 返回失败
//                map.put("resCode", "1");
//                return ServerResponse.getDataFailue(byTelno);
//            }
            session.setAttribute("smsCode",code);
            // 将短信码保存起来，使用全局变量
            storage.put(mobilePhone, code);
            // 返回成功
            return ServerResponse.getDataSuccess(code);
        }else {
            return byTelno;
        }

    }

    private ServerResponse registerGetMsg(String mobilePhone ,HttpSession session)   {
        // 返回参数
        Map<String, String> map = new HashMap<>();
        // 0 表示成功
        map.put("resCode", "0");

        ServerResponse byTelno = customerService.getByTelno(Long.valueOf(mobilePhone));
        if (byTelno.getCode() != 200) {
            // 生产随机短信码
            String code = RandomUtil.createRandNum();
            System.out.println("------ 验证码：" + code);

            // 2.使用工具类获取短信
            MsgUtil.sendMsg(mobilePhone, code);
            System.out.println("短信验证码："+code);//0
            // 3.判断短信发送结果
//            if (returnCode != 0) {
//                // 返回失败
//                map.put("resCode", "1");
//                return ServerResponse.getDataFailue(byTelno);
//            }
            session.setAttribute("smsCode",code);
            // 将短信码保存起来，使用全局变量
            storage.put(mobilePhone, code);
            // 返回成功
            return ServerResponse.getDataSuccess(code);
        }else {
            return byTelno;
        }

    }

    /**
     * 注册
     * @return
     */
    private ServerResponse doRegister(String mobilePhone,String passWord,HttpSession session){
        System.out.println(passWord);
        ServerResponse byTelno = customerService.getByTelno(Long.valueOf(mobilePhone));
        if (byTelno.getCode()!=200){
            Customer customer = new Customer();
            customer.setCustNo(Long.valueOf(mobilePhone));
            customer.setCustTelno(Long.valueOf(mobilePhone));
            customer.setCustPwd(passWord);
            customerService.save(customer);
            System.out.println(customer);
            ServerResponse byTelnoRegister = customerService.getByTelno(customer.getCustNo());
            session.setAttribute("loginCustomer",byTelnoRegister.getData());
            return byTelnoRegister;
        }
        return byTelno;
    }
}
